package student.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import org.zkoss.util.media.Media;

import cloudspace.vm.VM;


public class Fileupload extends org.zkoss.zul.Fileupload
{
    /**
     * 
     */
    private static final long serialVersionUID = 8880867841402405858L;


    public static void getAndSaveAs( File file ) throws IOException
    {
        getAndSaveAs( null, null, file );
    }


    public static void getAndSaveAs( String message, String title, File file ) throws IOException
    {
        try
        {
            Media media = get( message, title );
            if ( media != null )
            {
                saveMediaAsFile( media, file );
            }
        }
        catch ( InterruptedException localInterruptedException )
        {
        }
    }


    public static String getAndSaveToDir( File dir ) throws IOException
    {
        return getAndSaveToDir( null, null, dir );
    }


    public static String getAndSaveToDir( String message, String title, File dir ) throws IOException
    {
        try
        {
            Media media = get( message, title );
            return saveMediaToDir( media, dir );
        }
        catch ( InterruptedException localInterruptedException )
        {
        }

        return null;
    }


    public static String saveMediaToDir( Media media, File dir ) throws IOException
    {
        if ( media != null )
        {
            assert ( media.getName() != null ) : "Uploaded file has no name!";
            saveMediaAsFile( media, new File( dir, media.getName() ) );
            return media.getName();
        }
        return null;
    }


    public static void saveMediaAsFile( Media media, File file ) throws IOException
    {
        File root = new File(VM.currentVM().getLocalRoot(), file.getPath());
        if ( media.inMemory() )
        {
            if ( media.isBinary() )
            {
                FileOutputStream out = null;
                try
                {
                    out = new FileOutputStream( root );
                    out.write( media.getByteData() );
                }
                catch ( IOException e )
                {
                    throw new RuntimeException( e );
                }
                finally
                {
                    try
                    {
                        if ( out != null )
                        {
                            out.close();
                        }
                    }
                    catch ( IOException localIOException1 )
                    {
                    }
                }
                try
                {
                    if ( out == null )
                        return;
                    out.close();
                }
                catch ( IOException localIOException2 )
                {
                }

            }
            else
            {
                PrintWriter out = new PrintWriter(new FileOutputStream(root),false);
//                    IOHelper.createPrintWriter( file, false );
                out.print( media.getStringData() );
                out.close();
            }

        }
        else if ( media.isBinary() )
        {
            InputStream in = media.getStreamData();
            FileOutputStream out = null;
            try
            {
                out = new FileOutputStream( root );
                copyStream( in, out );
            }
            catch ( IOException e )
            {
                throw new RuntimeException( e );
            }
            finally
            {
                try
                {
                    if ( out != null )
                    {
                        out.close();
                    }

                }
                catch ( IOException localIOException3 )
                {
                }

                try
                {
                    if ( in != null )
                    {
                        in.close();
                    }
                }
                catch ( IOException localIOException4 )
                {
                }
            }
            try
            {
                if ( in == null )
                    return;
                in.close();
            }
            catch ( IOException localIOException6 )
            {
            }

        }
        else
        {
//            PrintWriter out = IOHelper.createPrintWriter( file, false );
            PrintWriter out = new PrintWriter(new FileOutputStream(root),false);
            Reader in = media.getReaderData();
            try
            {
                copyReader( in, out );
            }
            catch ( IOException e )
            {
                throw new RuntimeException( e );
            }
            finally
            {
                if ( out != null )
                {
                    out.close();
                }

                try
                {
                    if ( in != null )
                    {
                        in.close();
                    }
                }
                catch ( IOException localIOException7 )
                {
                }
            }
        }
    }


    private static void copyReader( Reader in, Writer out ) throws IOException
    {
        int BUFFER_SIZE = 65536;

        char[] b = new char[65536];
        int count = in.read( b );
        while ( count > -1 )
        {
            out.write( b, 0, count );
            count = in.read( b );
        }
        out.flush();
    }


    private static void copyStream( InputStream in, OutputStream out )
        throws IOException
    {
        int BUFFER_SIZE = 65536;

        byte[] b = new byte[65536];
        int count = in.read( b );
        while ( count > -1 )
        {
            out.write( b, 0, count );
            count = in.read( b );
        }
        out.flush();
    }
}